﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegrationTest
{
    public class TestDomainHandler : MarshalByRefObject
    {
        private TestFactory testFactory;

        public void Load()
        {
            testFactory = new TestFactory("testSection");
           
        }
        public void Run()
        {
            testFactory.Cases[0].CaseConf.RunTime = RunTime;
            testFactory.Run();
        }
        public int RunTime
        {
            get;
            set;
        }
        public TestDomainInfo GetInfo()
        {
            TestDomainInfo info = new TestDomainInfo();
            TestCase tc = testFactory.Cases[0];
            info.Completed = tc.Status == TestCaseStatus.Completed;
            info.Count = tc.GetCount();
            info.Success = tc.GetSuccess();
            info.Errors = tc.GetError();
            info.LastError = tc.GetLastError();
            info.LastSuccess = tc.GetLastSuccess();
            List<string> errmsg = new List<string>();
            foreach (Exception e in tc.GetLstError())
            {
                string msg = string.Format("--{0}-------------------------------------\r\n{1}\r\n\t{2}\r\n", DateTime.Now,e.Message, e.StackTrace);
                if(e.InnerException !=null)
                    msg += string.Format("\t{0}\r\n\t{1}\r\n",  e.InnerException.Message,e.InnerException.Source);
                
                errmsg.Add(msg);
            }
            info.ErrorLog = errmsg.ToArray();
            info.Status = tc.Status;
            return info;
        }
        public Counter[] GetCounter()
        {
            List<Counter> result = new List<Counter>();
            foreach (TestCounterConf tcc in testFactory.Cases[0].CaseConf.Counters)
            {
                Counter c = new Counter();
                c.Tag = tcc.Name;
                c.Value = tcc.Count;
                result.Add(c);
            }
            return result.ToArray();
        }
        public string GetReport()
        {
            return testFactory.Cases[0].ToString();
        }
    }
     [Serializable]
    public class Counter 
    {
        public string Tag { get; set; }
        public int Value { get; set; }
    }

    [Serializable]
    public class TestDomainInfo
    {
        public string Name { get; set; }

        public TestCaseStatus Status
        {
            get;
            set;
        }

        public int Count
        {
            get;
            set;
        }
        public int Success
        {
            get;
            set;
        }
        public string[] ErrorLog
        {
            get;
            set;
        }
        public int Errors
        {
            get;
            set;
        }
        public int LastSuccess
        {
            get;
            set;
        }
        public int LastError
        {
            get;
            set;
        }
        public bool Completed
        {
            get;
            set;
        }
    }
}
